package com.henu.rescue.controller;


import com.henu.rescue.entity.Organization;
import com.henu.rescue.entity.Station;
import com.henu.rescue.entity.Telephone;
import com.henu.rescue.mapper.OrgMapper;
import com.henu.rescue.service.OrgService;
import com.henu.rescue.service.StaService;
import com.henu.rescue.service.TelService;
import com.henu.rescue.utils.Page;
import com.henu.rescue.utils.Result;
import com.henu.rescue.utils.ResultEnums;
import com.henu.rescue.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.*;

@RestController
@RequestMapping("org/")
@Api("组织信息管理controller")
@Slf4j
public class OrgController {

    @Autowired
    private OrgService orgService;

    @Autowired
    private StaService staService;

    @Autowired
    private TelService telService;


    @ApiOperation("分页查询")
    @PostMapping("getAllByPage")
    public Result<Page<Organization>> getAllByPage(@RequestBody Page<Organization> page){
        page.setSortMethod("desc");
        String sortColumn = page.getSortColumn();
        if (StringUtils.isNotBlank(sortColumn)) {
            // 排序列不为空
            String[] sortColumns = {"org_name","org_tel","org_id"};
            List<String> sortList = Arrays.asList(sortColumns);
            if (!sortList.contains(sortColumn.toLowerCase())) {
                return new Result<>(ResultEnums.PARAMS_ERROR.getCode(), "排序参数不合法！");
            }
        }
        page= orgService.selectOrgInfoByPage(page);
        return new Result(ResultEnums.QUERY_SUCCESS,page);
    }

    @ApiOperation("添加组织")
    @PostMapping("addOrg")
    public Result addOrg(@RequestBody Organization org){
        System.out.println(org);
        if(org == null){
            return new Result(ResultEnums.ADD_FAILED,"车辆救援组织添加失败！");
        }
        //获取当前修改完后的station对应的的电话实体
        Telephone telephone = new Telephone();
        telephone.setTelType(1);
        telephone.setHostId(org.getOrgId());
        telephone.setTelNumber(org.getOrgTel());
        Telephone byTypeHostId = telService.selectByTypeHostId(telephone.getTelType(), telephone.getHostId());
       /* System.out.println(byTypeHostId);
        System.out.println(telephone);*/
        //判断电话号码是否被修改过
        System.out.println(telephone);
        System.out.println(byTypeHostId);
        if(!telephone.getTelNumber().equals(byTypeHostId.getTelNumber())){
            byTypeHostId.setTelNumber(telephone.getTelNumber());
            telService.updateTel(byTypeHostId);
        }
        Integer integer = orgService.addOrg(org);
        if(integer ==1 ){
            return new Result(ResultEnums.ADD_SUCCESS,"车辆救援组织添加成功！");
        }else {
            return new Result(ResultEnums.ADD_FAILED,"车辆救援组织添加失败！");
        }
    }

    @ApiOperation("删除组织")
    @DeleteMapping("delOrg/{orgId}")
    public Result delOrg(@PathVariable Integer orgId){
        if(orgId <= 0){
            return new Result(ResultEnums.PARM_ERROR,"救援组织id参数错误");
        }
        Integer integer = orgService.deleteOrg(orgId);
        if(integer != 1){
            return new Result(ResultEnums.DEL_ERROR,"未知错误");
        }
        /*//将救援组织下的救援服务点也删除  查询orgId下的救援服务点
        List<Station> stations = staService.selectStaByOrgId(orgId);
        //将救援服务点删除
        if(stations.size() > 0){
            for (int i = 0; i < stations.size(); i++) {
                Station station = stations.get(i);
                staService.delSta(station.getStaId());
            }
        }*/
        //将组织的电话删除 type:1
        telService.delTelFromOrg(1, orgId);

        return new Result(ResultEnums.SUCCESS,"救援组织删除成功");

    }

    @ApiOperation("更新组织")
    @PostMapping("updateOrg")
    public Result updateOrg(@RequestBody Organization organization){
        if(organization.getOrgId() <= 0){
            return new Result(ResultEnums.PARM_ERROR,"救援组织id参数错误");
        }
        Integer integer = orgService.updateOrg(organization);
        if(integer != 1){
            return new Result(ResultEnums.DEL_ERROR,"未知错误");
        }
        return new Result(ResultEnums.SUCCESS,"救援组织信息更新成功");
    }

    @ApiOperation("获取目前所有组织 id:名称")
    @PostMapping("getAllOrgName")
    public Result getAllOrgName(){
        List<Organization> allOrg = orgService.getAllOrg();
        Map<Integer, String> idName = new HashMap<>();
        for (int i = 0; i < allOrg.size(); i++) {
            Organization organization = allOrg.get(i);
            idName.put(organization.getOrgId(),organization.getOrgName());
        }
        return new Result(ResultEnums.SUCCESS,idName);
    }

}
